package com.ruoyi.custom.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.custom.domain.CustomRecord;
import com.ruoyi.custom.domain.vo.CustomVo;
import com.ruoyi.custom.service.ICustomRecordService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.CustomMapper;
import com.ruoyi.custom.domain.Custom;
import com.ruoyi.custom.service.ICustomService;

import static org.apache.commons.lang3.SystemUtils.getUserName;

/**
 * 客户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Service
public class CustomServiceImpl extends ServiceImpl<CustomMapper, Custom> implements ICustomService
{
    @Autowired
    private CustomMapper customMapper;
    @Autowired
    private ICustomRecordService customRecordService;
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询客户
     *
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public Custom selectCustomById(Long id)
    {
        return customMapper.selectCustomById(id);
    }

    /**
     * 查询客户列表
     *
     * @param custom 客户
     * @return 客户
     */
    @Override
    public List<CustomVo> selectCustomList(Custom custom)
    {
       List<CustomVo> list = customMapper.selectCustomList(custom);
       if(null!=list && list.size()>0){
           for(CustomVo vo: list){
               //通过customId获取最后一条记录
              CustomRecord record= customRecordService.selectLastRecord(vo.getId());
              vo.setRecord(record!=null?record.getRecord():null);
              vo.setLastTime(record!=null?record.getCreateTime():null);
              vo.setCreateBy(record!=null?record.getCreateBy():null);
           }
       }
        return list;
    }


    @DataScope(userAlias = "c")
    public List<CustomVo> selectCustomListData(Custom custom){
        List<CustomVo> list = customMapper.selectCustomListData(custom);
        if(null!=list && list.size()>0){
            for(CustomVo vo: list){
                //通过customId获取最后一条记录
                CustomRecord record= customRecordService.selectLastRecord(vo.getId());
                //获取还剩几天进入公共池
                String day = "0";
                List<SysDictData> datas= dictTypeService.selectDictDataByType("custom_public_time");
                if(null!=datas && datas.size()>0){
                    day =  datas.get(0).getDictValue();
                }
               CustomVo vo1 =  customMapper.getDayToPool(vo.getId());
                vo.setRecord(record!=null?record.getRecord():null);
                vo.setLastTime(record!=null?record.getCreateTime():null);
                vo.setCreateBy(record!=null?record.getCreateBy():null);
                vo.setDay(vo1!=null?vo1.getDay():day);
            }
        }
        return list;
    }
    /**
     * 新增客户
     *
     * @param custom 客户
     * @return 结果
     */
    @Override
    public int insertCustom(Custom custom)
    {
        custom.setCreateTime(DateUtils.getNowDate());
        return customMapper.insertCustom(custom);
    }

    /**
     * 修改客户
     *
     * @param custom 客户
     * @return 结果
     */
    @Override
    public int updateCustom(Custom custom)
    {
        return customMapper.updateCustom(custom);
    }

    public int sendCustom(CustomVo customVo){
       return customMapper.sendCustom(customVo);
    }

    public int getCustom(CustomVo customVo){
        customVo.setUserId(SecurityUtils.getUserId());
        customVo.setUserName(SecurityUtils.getUsername());
        customVo.setFollowTime(DateUtils.getNowDate());
        return customMapper.sendCustom(customVo);
    }
    /**
     * 批量删除客户
     *
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomByIds(Long[] ids)
    {
        return customMapper.deleteCustomByIds(ids);
    }

    /**
     * 删除客户信息
     *
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomById(Long id)
    {
        return customMapper.deleteCustomById(id);
    }

    public int updateCustomPool(){
        return customMapper.updateCustomPool();
    }

    //首页数量统计
    public Map<String,Object> getIndexCount(){
        return customMapper.getIndexCount();
    }

    public Map<String,Object> getIndexTableCount(){
        return customMapper.getIndexTableCount();
    }
}
